﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TCKQKL.FrontEnd.DataModel;
using TCKQKL.Service.LookupClient;
using System.Globalization;

namespace TCKQKL.FrontEnd.Web.Controls
{
    public partial class OrderPlacement : System.Web.UI.UserControl
    {
        public UpdatePanelUpdateMode UpdateMode
        {
            set { this.UpdatePanel2.UpdateMode = value; }
            get { return this.UpdatePanel2.UpdateMode; }
        }

        public OrdersOnlineUI order = null;
        public string orderType = null;
        public string newPrice
        {
            get
            {
                return (txtNewPrice != null) ? txtNewPrice.Text : "";
            }
        }

        public OrdersOnlineUI SubmitedOrder
        {
            set
            {
                if (value != null)
                {
                    if (!string.IsNullOrEmpty(value.OrderType))
                        ddlOrderType.SelectedValue = value.OrderType;
                    if (!string.IsNullOrEmpty(value.OrderSide))
                        ddlOrderSide.SelectedValue = value.OrderSide;
                    if (!string.IsNullOrEmpty(value.OrderClass))
                        ddlOrderClass.SelectedValue = value.OrderClass;
                    if (!string.IsNullOrEmpty(value.Symbol))
                        txtSymbol.Text = value.Symbol.ToUpper();
                    if (!string.IsNullOrEmpty(value.Price))
                        txtPrice.Text = value.Price;
                    if (value.Quantity > 0)
                        txtQuantity.Text = value.Price.ToString();
                    //                     if (!string.IsNullOrEmpty(value.OrderSpec))
                    //                         ddlOrderSpec.SelectedValue = value.OrderSpec;
                    //txtExpireDate.Text = value.ExpireDate.ToString("dd/MM/yyyy");
                }
            }
            get
            {
                if (string.IsNullOrEmpty(txtSymbol.Text) ||
                    string.IsNullOrEmpty(txtQuantity.Text))
                    return null;
                string accountNo = HttpContext.Current.User.Identity.Name;
                string orderSide = ddlOrderSide.SelectedValue;
                string symbol = txtSymbol.Text.Trim();
                string orderSpec = "NORMAL";// ddlOrderSpec.SelectedValue;
                string orderClass = ddlOrderClass.SelectedValue;
                string orderMode = "INTERNET";
                DateTime openDate = DateTime.Now;
                //                 CultureInfo cul = new CultureInfo("vi-VN");
                //                 DateTime expireDate = Convert.ToDateTime(txtExpireDate, cul);
                DateTime expireDate = DateTime.Today;
                string orderNo = "";
                DateTime timeStamp = DateTime.Now;
                string price = txtPrice.Text;
                int quantity = Convert.ToInt32(txtQuantity.Text);
                string orderType = ddlOrderType.SelectedValue;

                return new OrdersOnlineUI(0, accountNo, orderSide, symbol, orderSpec, orderClass, orderMode, openDate, expireDate, orderNo, timeStamp, price, quantity, orderType);

            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            orderType = (ddlOrderType.SelectedValue != null) ? ddlOrderType.SelectedValue : orderType;
            //this.txtExpireDate.Text = DateTime.Today.ToString("dd/MM/yyyy");
        }

        protected void txtSymbol_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtSymbol.Text))
            {
                ucQuote.StockSymbol = txtSymbol.Text;
                LSClient client = new LSClient();
                StocksInfoUI stk = client.GetStockInfo(txtSymbol.Text);
                client.Close();
                ucQuote.Update();
            }
        }

        protected void ibGetQuote_Click(object sender, ImageClickEventArgs e)
        {
            txtSymbol_TextChanged(sender, e);
        }

        public void Update()
        {
            if (order != null)
            {
                this.ddlOrderType.SelectedValue = "CHANGE";
                this.ddlOrderSide.SelectedValue = order.OrderSide;
                this.txtSymbol.Text = order.Symbol;
                this.txtQuantity.Text = order.Quantity.ToString();
                this.ddlOrderClass.SelectedValue = order.OrderClass;
                this.txtPrice.Text = order.Price;
                this.hfOrderId.Value = order.OrderId.ToString();
                this.txtQuantity.Enabled = false;
                this.txtSymbol.Enabled = false;
                this.txtPrice.Enabled = false;
                this.ddlOrderSide.Enabled = false;
                this.ddlOrderType.Enabled = false;
                this.ddlOrderClass.Enabled = false;
                orderType = "CHANGE";
            }
            this.UpdatePanel2.Update();
        }
    }
}